<template>
  <div>
    <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="来源">
          <el-select v-model="sname" placeholder="请选择" @change="Change2">
            <el-option
              v-for="item in sourceList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="需求">
          <el-input v-model="form.demand"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="公司">
          <el-input v-model="form.company"></el-input>
        </el-form-item>
        <el-form-item label="职位">
          <el-input v-model="form.job"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="Sex" placeholder="请选择" @change="Change1">
            <el-option
              v-for="item in yu"
              :key="item.lable"
              :label="item.value"
              :value="item.label"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker
            v-model="form.birthday"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="地区">
          <el-cascader
            size="large"
            :options="options"
            v-model="form.address"
            @change="addressChange"
          >
          </el-cascader>
        </el-form-item> -->
        <el-form-item label="微信">
          <el-input v-model="form.wxAccount"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOk('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { findById, update } from "@/api/toolUser";
import { getSoList } from "@/api/set";
import { regionData, CodeToText } from "element-china-area-data";
export default {
  name: "edit",
  data() {
    return {
      dialogVisible: false,
      imageUrl: "",
      //来源数组
      sourceList: [],
      form: {
        id: "",
        fid: "",
        name: "",
        demand: "",
        phone: "",
        company: "",
        job: "",
        sex: "",
        birthday: "",
        wxAccount: "",
        email: "",
        remark: "",
      },
      sname: "",
      Sex: "",
      options: regionData,
      selectedOptions: [],
      //性别
      yu: [
        {
          value: "男",
          label: "0",
        },
        {
          value: "女",
          label: "1",
        },
      ],
    };
  },
  methods: {
    addressChange(arr) {
      console.log(arr);
      console.log(CodeToText[arr[0]] + CodeToText[arr[1]] + CodeToText[arr[2]]);
      this.form.address =
        CodeToText[arr[0]] +
        " " +
        CodeToText[arr[1]] +
        " " +
        CodeToText[arr[2]];
      console.log(this.form.address);
    },
    showOne(e) {
      this.dialogVisible = true;
      this.form.id = e;
      let a = {
        id: this.form.id,
      };
      findById(a).then((res) => {
        if (res.status === 200) {
          console.log(res);
          for (var key in res.result.user) {
            if (res.result.user[key] == null) {
              res.result.user[key] = "";
            }
          }
          this.form.name = res.result.user.name;
          this.sname = res.result.user.sname;
          this.form.demand = res.result.user.demand;
          this.form.phone = res.result.user.phone;
          this.form.company = res.result.user.company;
          this.form.job = res.result.user.job;
          this.form.birthday = res.result.user.formatBirthday;
          this.form.address = res.result.user.address;
          this.form.wxAccount = res.result.user.wxAccount;
          this.form.email = res.result.user.email;
          this.form.remark = res.result.user.remark;
          if (res.result.user.sex == 0) {
            this.Sex = "男";
            this.form.sex = 0;
          } else {
            this.Sex = "女";
            this.form.sex = 1;
          }
          getSoList(a).then((res) => {
            if (res.status === 200) {
              console.log(res);
              this.sourceList = res.result;
              for (var i = 0; i < res.result.length; i++) {
                if (res.result[i].name == this.sname) {
                  this.form.fid = res.result[i].id;
                }
              }
              console.log(this.form.fid);
            }
          });
        }
      });
    },
    handleOk(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.form.phone != "") {
            var phone = /^1[3|4|5|7|8][0-9]\d{8}$/;
            if (!phone.test(this.form.phone)) {
              this.$message.warning("手机号格式错误, 请重新输入");
              return false;
            }
          }
          if (this.form.email != "") {
            var verify = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            if (!verify.test(this.form.email)) {
              this.$message.warning("邮箱格式错误, 请重新输入");
              return false;
            }
          }
          if (this.form.wxAccount != "") {
            var weixin = /^[0-9a-zA-Z]+$/;
            if (!weixin.test(this.form.wxAccount)) {
              this.$message.warning("微信号格式错误, 请重新输入");
              return false;
            }
          }

          update(this.form).then((res) => {
            if (res.status === 200) {
              this.$emit("Ok");
              this.dialogVisible = false;
              this.$message.success("编辑成功");
            }
          });
        }
      });
    },
    Change1(e) {
      console.log(e);
      this.form.sex = e;
    },
    Change2(e) {
      console.log(e);
      this.form.fid = e;
    },
  },
};
</script>

<style scoped></style>
